package com.peanut.baby.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.peanut.baby.AppConfig;
import com.peanut.baby.InitManager;

/**
 * Created by anonymouslystudio on 2017/7/13.
 */

public class PrefUtil {
    public static SharedPreferences prefs;

    public static SharedPreferences getPrefs() {
        if (prefs == null) {
            prefs = InitManager.getInstance().getCtx().getSharedPreferences(AppConfig.PrefConfig.PREF_NAME,
                    Context.MODE_PRIVATE);
        }
        return prefs;
    }

    /**
     * save String prefs
     *
     * @param key
     * @param value
     */
    public static void saveString(String key, String value) {
        getPrefs().edit().putString(key, value).commit();
    }

    /**
     * get String prefs
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getPrefs().getString(key, "");
    }

    /**
     * save int prefs
     *
     * @param key
     * @param value
     */
    public static void saveInt(String key, int value) {
        getPrefs().edit().putInt(key, value).commit();
    }

    /**
     * save int prefs
     *
     * @param key
     * @param value
     */
    public static void saveLong(String key, long value) {
        getPrefs().edit().putLong(key, value).commit();
    }

    /**
     * get int prefs
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getPrefs().getInt(key, -1);
    }

    public static int getIntDefaultZero(String key) {
        return getPrefs().getInt(key, 0);
    }

    public static long getLong(String key) {
        return getPrefs().getLong(key, 0L);
    }

    /**
     * save boolean prefs
     *
     * @param key
     * @param value
     */
    public static void saveBoolean(String key, boolean value) {
        getPrefs().edit().putBoolean(key, value).commit();
    }

    /**
     * get boolean prefs
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {
        return getPrefs().getBoolean(key, false);
    }


}
